//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"

func sortStr(str: String!) -> String {
    return String(str.sorted());
}

let str1 = sortStr(str: str);

func isStrNum(str: String) -> Bool {
    return Int(str) != nil;
}

let result = isStrNum(str: str);

//用数组实现栈
class Stack {
    var stack: [AnyObject];
    var isEmpty: Bool {
        return stack.isEmpty;
    }
    var peek: AnyObject? {
        return stack.last;
    }
    
    init() {
        stack = [AnyObject]();
    }
    
    func push(object: AnyObject) {
        stack.append(object);
    }
    
    func pop() -> AnyObject? {
        if (!isEmpty) {
            return stack.removeLast();
        } else {
            return nil;
        }
    }
}

// 给出一个整型数组和一个目标值,判断数组中是否有两个数之和等于目标值
let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
func twoSun(nums: [Int], _ target: Int) -> Bool {
    var set = Set<Int>();
    
    for num in nums {
        if (set.contains(target - num)) {
            return true;
        } else {
            set.insert(num);
        }
    }
    
    return false;
}

let a = twoSun(nums: nums, 117);

// 给定一个整型数组中有且仅有两个数之和等于目标值,求这两个数在数组中的序号.
func twoSum1(nums: [Int], _ target: Int) -> [Int] {
    var dict = [Int: Int]();
    
    for (i, num) in nums.enumerated() {
        if let lastIndex = (dict[target - num]) {
            return [i, lastIndex];
        } else {
            dict[num] = i;
        }
    }
    
    fatalError("No valid output");
}

let b = twoSum1(nums: nums, 18);

// 给出一个字符串, 要求将其按照单词顺序进行反转
fileprivate func _reverse<T>(_ chars: inout [T], _ start: Int, _ end: Int) {
    var start = start, end = end;
    
    while start < end {
        swap(&chars, start, end);
        start = start + 1;
        end = end - 1;
    }
}

fileprivate func swap<T>(_ chars: inout [T], _ p: Int, _ q: Int) {
    (chars[p], chars[q]) = (chars[q], chars[p]);
}

func reverseWords(s: String?) -> String? {
    guard let s = s else {
        return nil;
    }
    
    var chars = Array(s.characters);
    var start = 0;
    _reverse(&chars, 0, chars.count - 1);
    
    for i in 0..<chars.count {
        if i == chars.count - 1 || chars[i + 1] == " " {
            _reverse(&chars, start, i);
            start = start + 2;
        }
    }
    
    return String(chars);
}

// 实现链表
class ListNode {
    var val: Int;
    var next: ListNode?
    
    init(_ val: Int) {
        self.val = val;
        self.next = nil;
    }
}
class List {
    var head: ListNode?
    var tail: ListNode?
    
    // 尾插法
    func appendToTail(_ val: Int) {
        if (tail == nil) {
            tail = ListNode(val);
            head = tail;
        } else {
            tail!.next = ListNode(val);
            tail = tail!.next;
        }
    }
    
    // 头插法
    func appendToHead(_ val: Int) {
        if (head == nil) {
            head = ListNode(val);
            tail = head;
        } else {
            let temp = ListNode(val);
            temp.next = head;
            head = temp;
        }
    }
}

